import wxCharts from '../../components/wxcharts-min';
import { rpxToPx } from '../../utils/index';
import {WEAPP_VERSION} from '../../model/api';
import sensor from '../../utils/sa';

export default {
    namespace: 'index',
    state: {
        showTips: false,
        showType : 'none',
        notice: '',
        allData: {},
        series: [],
        count: 1,
        tip: null,
        config: {}
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        *init({ payload }, { put, call, select }) {

        
            yield put({
                type: 'getOilNotice'
            });
            yield put({
                type: 'getAllData'
            });

            const config = yield put.resolve({
                type : '$request/post',
                payload : {
                    url : '/api/config',
                }
            });

            if(config.version == WEAPP_VERSION){
                yield put({
                    type: 'oilLastTip'
                });
                yield put({
                    type: 'save',
                    payload: {showTips: true,config}
                });
            }else{
                yield put({
                    type: 'save',
                    payload: {showTips: false, tip: null,config}
                });
            }
        },
        /**
         * 获取油价提示
         * @param {*} param0 
         * @param {*} param1 
         */
        *getOilNotice({ payload }, { put  }){
            try{
                const notice = yield put.resolve({
                    type : '$request/post',
                    payload : {
                        url : '/api/getOilNotice',
                    }
                });
                yield put({
                    type: 'save',
                    payload: {
                        notice
                    }
                });
                
            }catch(e){
                
            }
            
            
        },
        /**
         * 获取统计数据
         * @param {*} param0 
         * @param {*} param1 
         */
        *getAllData({ payload }, { put  }){
            try{
                const carNumber = wx.getStorageSync('carNumber');
                const allData = yield put.resolve({
                    type : '$request/post',
                    payload : {
                        url : '/api/getAllData',
                        data: {carNumber}
                    }
                });
                
                yield put({
                    type: 'initCharts',
                    payload: {
                        allData
                    }
                });
                
            }catch(e){
                
            }

            
        },
        *oilLastTip({ payload }, { put, call, select }){
            const result = yield put.resolve({
                type : '$request/post',
                payload : {
                    url : '/api/getLastTips'
                }
            });
            const {count, tip} = result;
            yield put({
                type : 'save',
                payload : {
                    count, tip
                }
            });
            // 上报用户登录天数
            sensor.track('登录次数',{
                $element_name : JSON.stringify(count)
            });

        },
        * initCharts({payload},{put}){
            const {allData} = payload;
            allData.totalOil = allData.totalOil /100 ||0;
            allData.totalMain = allData.totalMain /100 || 0;
            let series = [{
                name: '加油',
                data: allData.totalOil,
            },{
                name: '保养',
                data: allData.totalMain,
            }];
            let consumList = [];
            let typeArr = ['停车','过路','保险','违章','维修','配饰','充电','其他'];
            allData.consumList = allData.consumList ||[];
            allData.consumList.forEach((item,index) => {
                allData.consumList[index] = item/100;
                consumList.push({
                    name: typeArr[index],
                    data: item/100
                });
            });
            // 按照价格排序
            consumList.sort((a,b) => a.data > b.data ? -1 : 1);
            let filterArr = consumList.filter(item => item.data > 0);
            // 数据组装
            if(filterArr.length >= 3){
                series = series.concat(filterArr);
            }else{
                series = series.concat(consumList.splice(0,3));
                series.push({name: '其他', data: 0});

            }
            yield put({
                type: 'save',
                payload: {
                    allData,
                    series
                }
            });
            const seriesCopy = JSON.parse(JSON.stringify(series));
            let total = 0;
            seriesCopy.forEach(item => total+= item.data||0);
            // UI特殊处理一下
            if(total == 0){
                seriesCopy.forEach(item => item.data = 0.01);
            }
            const cWidth = rpxToPx(480);
            const cHeight = rpxToPx(440);
            new wxCharts({
                canvasId: 'areaCanvas',
                type: 'pie',
                series: seriesCopy,
                width: cWidth,
                height: cHeight,
                legend: true,
                dataLabel: true
            });
        }
    }
};